package com.xw.wuliu;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.corey.handler.MyContentHandler;
import com.corey.model.SimpleSerList;
import com.corey.utils.FileUtils;
import com.corey.utils.HttpDownloader;
import com.corey.utils.Json;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ServiceListActivity extends Activity{
	
	private Button btBack = null;
	private ListView list = null;
	
	private Iterator<SimpleSerList> serLists;
	private String userPaName ="";
//	private ArrayList<String> mDate = null;
//	private ArrayAdapter<String> mAdapter = null;
	private ArrayList<HashMap<String, Object>> listItem;
	private SimpleAdapter listItemAdapter;
	private MyOtherHandler mOtherHandler;
	
	//不在同一线程的handler
	class MyHandler extends Handler{
		public MyHandler(){
			
		}
		public MyHandler(Looper looper){
			super(looper);
		}
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			
			Bundle b = msg.getData();
			int flag = b.getInt("flag");
			if (flag == 1) {
				loadData();
			}else{
				
			}
		}
	}
	//在同一线程的handler
	class MyOtherHandler extends Handler{
		public MyOtherHandler(){
			
		}
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			
			if (msg.what == 1) {
//				System.out.println("test handler"+"--------" + Thread.currentThread().getId());
				listItemAdapter.notifyDataSetChanged();
//				list.invalidate();
			}else{
				
			}
		}
	}
//	private MyHandler mHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.servicelist);

		mOtherHandler = new MyOtherHandler();
		userPaName = getIntent().getExtras().getString("userPaName");

		findViews();
		setListensers();
		downLoadInfoInHandler(userPaName);
	}
	
	private void downLoadInfoInHandler(String userPaName){
		
//		mHandler = new MyHandler();
		
//		System.out.println("act--->"+ Thread.currentThread().getId());
		HandlerThread handlerThread = new HandlerThread("handler_thread");
		handlerThread.start();
		MyHandler myHandler = new MyHandler(handlerThread.getLooper());
		Message msg = myHandler.obtainMessage();
		Bundle b = new Bundle();
		b.putInt("flag", 1);
		b.putString("userPaName", userPaName);
		msg.setData(b);
		msg.sendToTarget();
		
	}
	
	private void loadData(){
		List<String> strs = new ArrayList<String>();
		String resultString = "";
		
		HttpDownloader hd = new HttpDownloader();
		String resultStr = hd.download(FileUtils.urlStr+"ServerList?userPsname="+userPaName);
//		System.out.println("URL:"+ FileUtils.urlStr+"ServerList?userPsname="+userPaName);
//		System.out.println("result:"+resultStr);
		
		try{
			//����һ��SAXParserFactory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			XMLReader reader = factory.newSAXParser().getXMLReader();
			//ΪXMLReader�������ݴ�����
			MyContentHandler myContentHandler = new MyContentHandler(strs);
			reader.setContentHandler(myContentHandler);
			//��ʼ�����ļ�
			reader.parse(new InputSource(new StringReader(resultStr)));
			for (Iterator iterator = strs.iterator(); iterator.hasNext();) {
				resultString = (String) iterator.next();
//				System.out.println("Str@@@@"+ resultString);
			}
			
			Json kit = new Json();
			serLists = kit.parseUserFromJson(resultString);
			
			setListView();
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	private void findViews(){
		btBack = (Button) findViewById(R.id.btback);
		list = (ListView) findViewById(R.id.lvSerList);
		initListView();
	}
	
	private void initListView(){
		
		listItem = new ArrayList<HashMap<String, Object>>();
		
		listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.serlist_list,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "serListID", "serListNa", "serListAdd" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.serListID_2, R.id.serListNa_2, R.id.serListAdd_2 });

		//添加并现实
		list.setAdapter(listItemAdapter); 
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//setTitle("点击第"+arg2+"个项目"); 
				//System.out.println("listitem 点击");
				Intent i = new Intent();
				i.setClass(ServiceListActivity.this, DetailOfSerListAct.class);
				startActivity(i);
			}  
			
		});
	}

	
	private void setListensers(){
		
		btBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ServiceListActivity.this.finish();
			}
		});
	}
	
	//重新设置list
	private void setListView(){

		if(listItem.size() != 0){
//			System.out.println("listItem2:"+listItem.size());
			listItem.clear();
		}
		
		while(serLists.hasNext()){
			SimpleSerList user = serLists.next();
			
			HashMap<String, Object> map = new HashMap<String, Object>();  
			map.put("serListID", user.getSerListNo());
			map.put("serListNa", user.getSerListName());
			map.put("serListAdd", user.getSerListAddr());
			listItem.add(map);  
			
//			System.out.println(user.getSerListNo()+ "-------" + user.getSerListName() + "-------" + user.getSerListAddr());
		}
		
		Message msg = mOtherHandler.obtainMessage();
		msg.what = 1;
		msg.sendToTarget();
		
//		listItemAdapter.notifyDataSetChanged();
//		list.invalidate();
//		System.out.println("test2222222");
	}
}
